package com.example.jh352160.surfingtv.starsight.customView;

import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.jh352160.surfingtv.R;
import com.example.jh352160.surfingtv.guid.GuidActivity;
import com.example.jh352160.surfingtv.network.item.RequestItem;
import com.example.jh352160.surfingtv.network.item.StarsightItem;
import com.example.jh352160.surfingtv.network.bean.StarsightLikeBean;
import com.example.jh352160.surfingtv.network.retrofit.CommonRetrofit;
import com.example.jh352160.surfingtv.network.service.StarsightLikeService;
import com.example.jh352160.surfingtv.starsight.StarsightMain;
import com.example.jh352160.surfingtv.thematic_details.ThematicDetailsPage;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

/**
 * Created by jh352160 on 2016/7/21
 */

public class StarsightImage extends RelativeLayout {

    private ImageView bigImage;
    private Button imageButton;
    private Boolean isLike = false;
    private TextView pricetext;
    private Context context;
    StarsightItem.DataBean.ItemsBean itemsBean;

    public StarsightImage(Context context, StarsightItem.DataBean.ItemsBean itemsBean) {
        super(context);
        this.context = context;
        this.itemsBean = itemsBean;
        init();
    }

    public StarsightImage(final Context context) {
        super(context);
        this.context = context;
        init();
    }

    private void init() {
        LayoutInflater.from(context).inflate(R.layout.starsight_image, this);
        bigImage = (ImageView) findViewById(R.id.bigImage);
        imageButton = (Button) findViewById(R.id.imageButton);
        imageButton.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {

                Retrofit retrofit = CommonRetrofit.getRetrofit();
                StarsightLikeService likeService = retrofit.create(StarsightLikeService.class);
                Call<RequestItem> call = likeService.likeStar(new StarsightLikeBean(itemsBean.getContentId() + ""));
                call.enqueue(new Callback<RequestItem>() {
                    @Override
                    public void onResponse(Call<RequestItem> call, Response<RequestItem> response) {
                        if (GuidActivity.isLogin) {
                            if (isLike) {
                                imageButton.setBackgroundResource(R.drawable.starsight_unpraised);
                                imageButton.setText(itemsBean.getLikeQuantity() + "");
                            } else {
                                imageButton.setBackgroundResource(R.drawable.starsight_praised);
                                imageButton.setText((itemsBean.getLikeQuantity() + 1) + "");
                                Animation priceAnim = AnimationUtils.loadAnimation(context, R.anim.price);
                                pricetext = (TextView) findViewById(R.id.pricetext);
                                pricetext.setVisibility(VISIBLE);
                                pricetext.startAnimation(priceAnim);
                                pricetext.setVisibility(INVISIBLE);
                            }
                            isLike = !isLike;
                        } else {
                            ((StarsightMain.FragmentListener) context).toLogin();
                        }
                    }

                    @Override
                    public void onFailure(Call<RequestItem> call, Throwable t) {
                        Log.e("Retrofit Error", t.toString());
                    }
                });
            }
        });

        bigImage.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, ThematicDetailsPage.class);
                intent.putExtra("detailUrl", itemsBean.getDetailUrl());
                intent.putExtra("title", itemsBean.getTitle());
                intent.putExtra("favoriteCount", itemsBean.getFavoriteCount());
                intent.putExtra("isfavorite", itemsBean.getFavorite() == 1);
                intent.putExtra("contentId", itemsBean.getContentId());
                context.startActivity(intent);
            }
        });
    }

    public void setImage(String imageUrl, int favoriteCount, int favorite, int contentId, String detailUrl, String title) {
        Glide.with(context).load(imageUrl).into(bigImage);
        if (!(itemsBean.getLikeStatus() == 0)) {
            imageButton.setBackgroundResource(R.drawable.starsight_praised);
        }
        imageButton.setText(itemsBean.getLikeQuantity() + "");
    }
}